package cn.z2huo.algorithm.binarytree;

// 求二叉树的最小深度
// 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
public class MinimumDepth {
    public int getMinimumDepth(TreeNode root){
        if (root == null) {
            return 0;
        }
        if (root.getLeft() == null) {
            return getMinimumDepth(root.getRight())+1;
        }
        if (root.getRight() == null) {
            return getMinimumDepth(root.getLeft())+1;
        }
        return Math.min(getMinimumDepth(root.getLeft()), getMinimumDepth(root.getRight()))+1;
    }

    public static void main(String[] args){
        BinaryTree bt = new BinaryTree();
//        int[] array = {1,2,3,4,5,3,4,5,6,6,7,7,7,7,7};
        int[] array = {1,2,3,4,5,0,0,0,0,6,7};
//        int[] array = {1,2};
//        int[] array = {1,0,2};
//        int[] array = {1,0,2,0,3,0,4,0,5};
//        int[] array = {1,2,0,3,0,4,0,5};
        TreeNode root = bt.createTree(array);

        MinimumDepth md = new MinimumDepth();
        System.out.println(md.getMinimumDepth(root));
    }
}
